<?php
namespace App\YlyService;

class typography{

    static $versionOld = array('k1', 'k2', 'k3', 'old_print');
    static $version58mm = array('k2s', 'k3s', 'k4', 'm1','k6');
    static $version80mm = array('w1');

    public static function checkArray($params)
    {
        if(in_array($params, self::$versionOld)){
            return 0;
        }
        if(in_array($params, self::$version58mm)){
            return 1;
        }
        if(in_array($params, self::$version80mm)){
            return 2;
        }
        return false;
    }


    public static function center($size, $option)
    {
        $data = "<center>".$option."</center>";
        if($size == 2){
            $data = "<FS><center>".$option."</center></FS>";
        }
        if($size == 3){
            $data = "<FS2><center>".$option."</center></FS2>";
        }
        return $data;
    }

    public static function table($size)
    {
        $table = "<table>";
        $endTable = "</table>";
        if($size == 2){
            $table = "<FS><table>";
            $endTable = "</table></FS>";
        }
        if($size == 3){
            $table = "<FS2><table>";
            $endTable = "</table></FS2>";
        }
        return array($table, $endTable);
    }


    public static function FS($version, $size, $option)
    {
        $per = 'FS';
        $n = $per;
        //新版
        if ($version == 1 || $version == 2) {
            if ($size == 1) {
                $FS1 = '';
                $FS2 = '';
            } else if ($size == 2) {
                $FS1 = '<' . $n . '>';
                $FS2 = '</' . $n . '>';
            } else if ($size == 3) {
                $FS1 = '<' . $n . '2' . '>';
                $FS2 = '</' . $n . '2' . '>';
            }
            return $FS1 . $option . $FS2 . "\n";
            //老版本
        } else {
            if ($size == 1) {
                $FS = '';
            } else {
                $arr = self::mbStrSplit($option, 32);
                $FS = '@@2';
                $option = implode("\n@@2", $arr);
            }
            return $FS . $option . "\n";
        }
    }

    static function getStrRepeat($version, $symbol, $option = '')
    {
        $char = "。、！？：；﹑•＂…‘’“”〝〞∕¦‖—　〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔！¡？¿﹖﹌﹏﹋＇´ˊˋ―﹫︳︴¯＿￣﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼（）";
        preg_match_all("/[\x{4e00}-\x{9fa5}'.$char.']+/u", $option, $chinese);
        if (
            empty($chinese) ||
            empty($chinese[0][0])
        ) {
            $optionLen = strlen($option);
        } else {
            $optionLen = strlen($option) / 3 * 2;
        }
        $totalLen = ($version == 2) ? 48 : 32;
        $length = ($totalLen - $optionLen) / 2;
        return str_repeat($symbol, $length) . $option . str_repeat($symbol, $length);
    }

    static function mbStrSplit($string, $len = 1)
    {
        $start = 0;
        $string = iconv('UTF-8', 'gbk//IGNORE', $string);
        $strlen = strlen($string);
        while ($strlen) {
            $array[] = iconv('gbk', 'UTF-8', mb_strcut($string, $start, $len, 'gbk'));
            $string = mb_strcut($string, $len, $strlen, 'gbk');
            $strlen = strlen($string);
        }
        return $array;
    }


    public static  function filterUtf8($str)
    {
        /*utf8 编码表：
        * Unicode符号范围           | UTF-8编码方式
        * u0000 0000 - u0000 007F   | 0xxxxxxx
        * u0000 0080 - u0000 07FF   | 110xxxxx 10xxxxxx
        * u0000 0800 - u0000 FFFF   | 1110xxxx 10xxxxxx 10xxxxxx
        *
        */
        $re = '';
        $str = str_split(bin2hex($str), 2);

        $mo =  1<<7;
        $mo2 = $mo | (1 << 6);
        $mo3 = $mo2 | (1 << 5);         //三个字节
        $mo4 = $mo3 | (1 << 4);          //四个字节
        $mo5 = $mo4 | (1 << 3);          //五个字节
        $mo6 = $mo5 | (1 << 2);          //六个字节


        for ($i = 0; $i < count($str); $i++)
        {
            if ((hexdec($str[$i]) & ($mo)) == 0)
            {
                $re .=  chr(hexdec($str[$i]));
                continue;
            }

            //4字节 及其以上舍去
            if ((hexdec($str[$i]) & ($mo6) )  == $mo6)
            {
                $i = $i +5;
                continue;
            }

            if ((hexdec($str[$i]) & ($mo5) )  == $mo5)
            {
                $i = $i +4;
                continue;
            }

            if ((hexdec($str[$i]) & ($mo4) )  == $mo4)
            {
                $i = $i +3;
                continue;
            }

            if ((hexdec($str[$i]) & ($mo3) )  == $mo3 )
            {
                $i = $i +2;
                if (((hexdec($str[$i]) & ($mo) )  == $mo) &&  ((hexdec($str[$i - 1]) & ($mo) )  == $mo)  )
                {
                    $r = chr(hexdec($str[$i - 2])).
                        chr(hexdec($str[$i - 1])).
                        chr(hexdec($str[$i]));
                    $re .= $r;
                }
                continue;
            }



            if ((hexdec($str[$i]) & ($mo2) )  == $mo2 )
            {
                $i = $i +1;
                if ((hexdec($str[$i]) & ($mo) )  == $mo)
                {
                    $re .= chr(hexdec($str[$i - 1])) . chr(hexdec($str[$i]));
                }
                continue;
            }
        }
        return $re;
    }


}